import { Component, OnInit } from "@angular/core";
import { ModalController, ActionSheetController } from '@ionic/angular';
import { NotarizationInfoService } from '../../../services/notarization-info.service';
import { HttpService } from '../../../services/http.service';
import { Router } from '@angular/router';
import { BaseUiService } from '../../../services/base-ui.service';
import { LoginInfoService } from '../../.././services/login-info.service';
import { commService } from '../../../services/comm.service';

@Component({
  selector: "app-topay",
  templateUrl: "./topay.page.html",
  styleUrls: ["./topay.page.scss"]
})
export class TopayPage implements OnInit {
  public orderId:any ;
  public notaryItems:any;
  public orderNo: '';
  public notaryNum: any;
  constructor(
    public router: Router,
    public HttpService: HttpService,
    public NotarizationInfoService: NotarizationInfoService,
    public BaseUiService: BaseUiService,
    public LoginInfoService: LoginInfoService,
    public commService: commService,
    public baseUiService: BaseUiService,
    public actionSheetController: ActionSheetController,
  ) {}
  
  ngOnInit() {
    this.orderId = this.NotarizationInfoService.getOrder()
    // this.orderId = '170215f6-35d7-480a-9a01-dbbc9bbd4c9f';
    this.seach()
  }
  
  seach(){
    let p = {
      unitGuid:this.orderId
    }
    this.HttpService.getNotaryorderById(p).then(res =>{
      if(res['code']==200){
        let { notaryItems } = res['item']
        this.notaryItems = notaryItems
        this.orderNo = res['item']['order']['orderNo'];
        this.notaryNum = res['item']['order']['notaryNum'];
      }else{
        alert(res['msg'])
      }
    }).catch(err =>{
      alert(err)
    })
  }

  //去详情
  goOrder(){
    this.router.navigate(['/inquire/order'], { queryParams: {
      orderId:this.orderId,
      // notaryStateName:info.notaryStateName
    } })
  }
  //去付款
  goFlow(){
    //支付
    let info = {
      unitGuid: this.orderId,
      notaryState: 30,
      payType: 'pt001'
    }
    this.HttpService.doSetState(info).then(
      res => {
        this.BaseUiService.showToast('付款成功','success','top')
        this.router.navigate(['/tabs/tab2'])
      }
    )
  }

  async replay() {
    const actionSheet = await this.actionSheetController.create({
      header: '支付',
      cssClass:"relay",
      buttons: [{
        text: '支付宝支付',
        role: 'share',
        icon: 'iconfont icon-zhifubao1',
        handler: () => {
          this.doVideologSetState();
        }
      }, {
        text: '微信支付(暂不支持)',
        role: 'share',
        icon: 'iconfont icon-weixinzhifu1',
        handler: () => {
          // this.doVideologSetState();
        }
      }, {
        text: '关闭',
        icon: 'close',
        role: 'cancel',
        handler: () => {
        }
      }]
    });
    await actionSheet.present();
  }

  doVideologSetState(){
    let param={
      outTradeNo: this.orderNo 
    }
    this.HttpService.doSign(param).then(res=>{  
      if(res['code']=="200"){
        this.commService.aliPay(res['item']).subscribe(res=>{
          console.log("---------------支付-------------------------------------",res)
          console.log(res.trim()=="5001")
            if(res.trim() =="5001"){
              this.baseUiService.showToast('支付失败','customblue','top');
            }else{
              this.goFlow();
            }
        })
      }
    })
  }
}
